# -*- encoding:utf-8 -*-
import time
import datetime
import json
import requests
from constant import constant_define

from dao.MySqlDAL import MySqlDAL
from config.Config import Config
import utils.CookieUtils as cu
__author__ = 'shudong.msd'


'''
    抓取 组合名称

'''

class CubeNameCrawler(object):

    # tablename
    __tablename = 'tb_cube'

    __code_list = ['GB2312','GBK','utf-8','ascii','ANSI']

    # 和排行榜抓取的保持同步
    __record_count = 100

    __sqlUtil = MySqlDAL()

    __headers = {
            'Accept':'application/json, text/javascript, */*; q=0.01',
            'Accept-Encoding':'gzip, deflate, sdch',
            'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
            'cache-control':'no-cache',
            'Connection':'keep-alive',
            'User-Agent': constant_define.user_agent,
            'Host':'xueqiu.com',
            'X-Requested-With':'XMLHttpRequest',
            'Cookie': cu.init_cookie()
    }



    def init_target(self,target):
        self.__target = target
        self.__cube_list = self.__get_final_list()


    def set_cube_list(self,cube_list):
        self.__cube_list = cube_list

    def get_cube_list(self):
        return self.__cube_list

    def __get_total_count(self):
        return requests.get(self.__target,params={'count':1},headers=self.__headers).json()['totalCount']

    # 得到响应信息
    def __get_response(self):
        # count = self.__get_total_count()
        time.sleep(1)
        # return requests.get(self.__target,params={'count':count},headers=self.__headers).json()
        return requests.get(self.__target,params={'count':self.__record_count},headers=self.__headers).json()

    # 获得所有的数据dict
    def __get_final_list(self):
        res_dic = self.__get_response()
        res_list = res_dic['list']

        return res_list

    # 写入db rank
    def writeToDB(self):
        cube_list = self.__cube_list

        # 插入rank
        keys = ['symbol','name','date','update_time']
        source = []
        for row in cube_list:
            tmp = list()
            tmp.append(row['symbol'])
            tmp.append(row['name'])
            tmp.append(datetime.date.today())
            tmp.append(time.time())
            source.append(tmp)

        return self.__sqlUtil.insert_many_batch(keys,source,self.__tablename,60)


    # 更新db
    def updateDB(self):
        # data_source=[(修改值1),(2),(3)...(过滤条件1),(2)...] ,
        cube_list = self.__cube_list
        # 插入rank
        keys = ['name','update_time']
        filter_keys=['symbol','date']
        source = []
        for row in cube_list:
            tmp = list()
            tmp.append(row['name'])
            tmp.append(time.time())

            tmp.append(row['symbol'])
            tmp.append(datetime.date.today())
            source.append(tmp)

        return self.__sqlUtil.update_many_batch(keys,source,self.__tablename,filter_keys,60)
